package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author Xutao Bai
 * @Date created in 2020/1/3
 * @description
 * @Version 1.0
 */

@RestController
@RequestMapping("menu")
@Slf4j
public class MenuController {
    @Reference
    private MenuService menuService;

    /**
     * 查询所有
     * @return
     */
    @GetMapping("findAllList")
    public Result findAllList(){
        List<Menu> menus = menuService.findAllList();
        return new Result(true, MessageConst.ACTION_SUCCESS,menus);}

    /**
     * 新增菜单
     * @param menu
     * @param children
     * @return
     */
    @PostMapping("add")
    public Result add(@RequestBody Menu menu, Integer[] children) {
        log.info("[菜单--新增]data{},parentId:{}", menu, children);
        Menu parentMenu = menuService.findByParentMenuId(children[0]);
        String path = parentMenu.getPath();
        Integer count = menuService.count2MenuByPath("/" + path);
        Integer newMenuCount = count + 1;
        menu.setParentMenuId(children[0]);
        menu.setPath("/"+path+"-"+newMenuCount);
        menu.setPriority(newMenuCount);
        menu.setLevel(2);
        menuService.add(menu);
        return new Result(true,MessageConst.ACTION_SUCCESS);
    }

    /**
     * 显示所有菜单
     *
     * @param
     * @return
     */
    @GetMapping("findAll")
    public Result findAll() {
        //查询所有 菜单
        List<Menu> menulist = menuService.findAll();

        return new Result(true, MessageConst.GET_MENU_SUCCESS, menulist);
    }

    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @GetMapping("findPage")
    public Result findPage(QueryPageBean queryPageBean) {
        log.info("[菜单分页查询]date:{}", queryPageBean);
        PageResult pageResult = menuService.pageQuery(queryPageBean);
        return new Result(true, MessageConst.ACTION_SUCCESS, pageResult);
    }

    /**
     * 根据id 删除
     *
     * @param id
     * @return
     */
    @RequestMapping("delete")
    public Result delete(Integer id) {
        log.info("[菜单删除,根据id]id:{}", id);
        menuService.delete(id);
        return new Result(true, MessageConst.ACTION_SUCCESS);

    }

    /**
     * 编辑
     * @param menu
     * @param children
     * @return
     */
    @PostMapping("edit")
    public Result edit(@RequestBody Menu menu, Integer[] children) {
        log.info("[菜单-编辑]data:{},children:{}", menu,children);
        delete(menu.getId());
        Menu parentMenu = menuService.findByParentMenuId(children[0]);
        String path = parentMenu.getPath();
        Integer count = menuService.count2MenuByPath("/" + path);
        Integer newMenuCount = count + 1;
        Menu newMenu = new Menu();
        newMenu.setName(menu.getName());
        newMenu.setLinkUrl(menu.getLinkUrl());
        newMenu.setDescription(menu.getDescription());
        newMenu.setParentMenuId(children[0]);
        newMenu.setPath("/"+path+"-"+newMenuCount);
        newMenu.setPriority(newMenuCount);
        newMenu.setLevel(2);
        menuService.add(newMenu);
        return new Result(true, MessageConst.ACTION_SUCCESS);
    }

    /**
     * 根据父级id查询-菜单
     *
     * @param id
     * @return
     */
    @GetMapping("findchildrenByMenuId")
    public Result findchildrenByMenuId(Integer id) {
        log.info("[根据父级id查询-菜单]ParentMenuId:{}", id);
        Integer parentMenuId = menuService.findchildrenByMenuId(id);
        return new Result(true, MessageConst.GET_MENU_SUCCESS, parentMenuId);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("findById")
    public Result findById(Integer id) {
        log.info("[根据id查询-菜单]Id:{}", id);
        Menu menuid = menuService.findById(id);
        return new Result(true, MessageConst.GET_MENU_SUCCESS, menuid);
    }
}
